//
//  ELHomeTableViewCell.swift
//  SinaWeiBo
//
//  Created by jiwang on 15/12/20.
//  Copyright © 2015年 jiwang. All rights reserved.
//

import UIKit

class ELHomeTableViewCell: UITableViewCell {

    /// 配图宽度约束
    var pictureViewWidthCon: NSLayoutConstraint?
    
    /// 配图高度约束
    var pictureViewHeightCon: NSLayoutConstraint?
    
    var status : ELStatus? {
        didSet {
            topView.status = status
            
            contentLabel.text = status?.text
            
            //根据图片数量更改配图的高度和宽度
            pictureView.status = status
            
            let size : CGSize = pictureView.caculateViewSize()
            
            pictureViewWidthCon?.constant = size.width
            pictureViewHeightCon?.constant = size.height
        }
    }
    
    //自动返回行高
    func rowHeight(status : ELStatus) -> CGFloat {
        
        //根据不同的模型计算行高 设置cell的status,cell会根据status里面的内容重新设置每个控件的内容
        self.status = status
        
        //设置后更新约束 重新布局
        layoutIfNeeded()
        
        let height = CGRectGetMaxY(bottomView.frame)
        
        return height
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func prepareUI () {
        
        //添加子控件
        contentView.addSubview(seperaView)
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(bottomView)
        contentView.addSubview(pictureView)
        
        seperaView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: self.contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 10))
        
        //顶部视图添加约束
        topView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: seperaView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        
        //发表内容添加约束
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: 8, y: 8))
        
        //约束文字的宽度
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width - 2 * 8))
        
////        cell的高度根据文字底部适应
//        contentView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 8))
        
        //为配图添加约束
       let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 290, height: 290), offset: CGPoint(x: 0, y: 8))

        pictureViewWidthCon = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
        pictureViewHeightCon = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
        
        //为底部视图添加约束
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -8, y: 8))

//        contentView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bottomView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    }

    //MARK: - 懒加载
    ///顶部视图
    lazy var topView : ELHomeCellTopView = ELHomeCellTopView()
    
    ///微博内容
    lazy var contentLabel : UILabel = {
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        
        return label
    }()
    
    ///底部视图
    lazy var bottomView : ELHomeBottomView = ELHomeBottomView()
    
    ///cell的间隔栏
    lazy var seperaView : UIView = {
        let view = UIView()
        
        view.backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        return view
    }()
    
    ///微博配图视图
    lazy var pictureView : ELPictureView = ELPictureView()
}
